﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace homework2
{
    public partial class Form1 : Form
    {
        private DataBaseTool _dataBaseTool = new DataBaseTool();
        public Form1()
        {
            InitializeComponent();
        }

        private void account_Enter(object sender, EventArgs e)
        {
            if (accountHasText == false)
                this.account.Text = "";
            this.account.ForeColor = Color.Black;
        }

        private void account_Leave(object sender, EventArgs e)
        {
            if (this.account.Text == "")
            {
                this.account.Text = "请输入学号";
                this.account.ForeColor = Color.LightGray;
                accountHasText = false;
            }
            else
            {
                accountHasText = true;
            }
        }
        
        private void password_Enter(object sender, EventArgs e)
        {
            if (passwordHasText == false)
                this.password.Text = "";
            this.password.ForeColor = Color.Black;
            this.password.PasswordChar = '·';
        }
        
        private void password_Leave(object sender, EventArgs e)
        {
            if (this.password.Text == "")
            {
                this.password.Text = "请输入密码";
                this.password.ForeColor = Color.LightGray;
                this.password.PasswordChar = (char) 0;
                passwordHasText = false;
            }
            else
            {
                passwordHasText = true;
            }
        }

        /// <summary>
        /// 登录
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void login_click(object sender, EventArgs e)
        {
            if (_dataBaseTool.login(this.account.Text, this.password.Text))
            {
                this.Hide();
                mainFrame frame = new mainFrame(this.account.Text);
                frame.Show();
                
            }
            else
            {
                MessageBox.Show("登陆失败");
            }
        }

        private void register_click(object sender, EventArgs e)
        {
            if (this.account.Text == "请输入学号" || this.password.Text == "请输入密码")
            {
                MessageBox.Show("注册失败");
                return;
            }
            if (_dataBaseTool.register(this.account.Text, this.password.Text))
            {
                MessageBox.Show("注册成功");
            }
            else
            {
                MessageBox.Show("注册失败");
            }
        }
    }
}